package com.kky.pp.activity.me.info

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.custom.base.config.BaseConfig
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.gyf.immersionbar.ImmersionBar
import com.kky.pp.App
import com.kky.pp.common.Constant
import com.kky.pp.R
import com.kky.pp.base.TitleMvpBaseActivity
import com.kky.pp.bean.TOListBean
import com.kky.pp.databinding.ActivityUserInfoBinding
import com.kky.pp.popup.TOListPopup
import com.kky.pp.popup.SelectPhotoPopup
import com.kky.pp.popup.UpdateAboutPopup
import com.kky.pp.popup.NickNameModifyPopup
import com.kky.pp.entity.UserProfileEntity
import com.kky.pp.util.CollectionUtils
import com.kky.pp.util.TextUtil
import com.kky.pp.view.TagCloudView
import com.kky.pp.view.upImage.ImageClickListener
import com.kky.pp.view.upImage.UpImageView
import io.rong.imkit.SpName

class ProfileInfoActivity :
    TitleMvpBaseActivity<ProfileInfoContact.View, ProfileInfoPresenter, ActivityUserInfoBinding>(
        ActivityUserInfoBinding::inflate
    ),
    ProfileInfoContact.View {
    override fun bindLayout(): Int {
        return R.layout.activity_user_info
    }

    override fun setFullScreen(): Boolean {
        return true
    }

    override fun setTitle(): String {
        return App.app?.resources?.getString(R.string.edit_profile)!!
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(Constant.user_info_url)
            }
        }, object : SDOkHttpResoutCallBack<UserProfileEntity>() {
            override fun onSuccess(entity: UserProfileEntity) {

                for (i in 0 until entity.data.images.size) {
                    Glide.with(mActivity).load(entity.data.images[i])
                        .diskCacheStrategy(DiskCacheStrategy.ALL).preload()
                }
                if (CollectionUtils.isNotEmpty(entity.data.turnOnsList)) {
                    repeat(entity.data.turnOnsList.size) {
                        Glide.with(mActivity).load(entity.data.turnOnsList[it].imageUrl)
                            .diskCacheStrategy(DiskCacheStrategy.ALL).preload()
                    }
                }

                BaseConfig.getInstance.setString(SpName.userCode, entity.data.userCode)

                val bundle = Bundle()
                bundle.putSerializable("entity", entity.data)
                val turnOnsData = setTurnOnsData(entity.data.turnOnsList)
                viewBinding.viewTurnOnsUserInfo.setCLick(this@ProfileInfoActivity, entity) {
                    setTurnOnsData(it)
                }
//                viewBinding.viewTurnOnsUserInfo.textTurnOns?.setOnClickListener {
//                    TOListPopup.openDialog(this@ProfileInfoActivity, entity.data.turnOnsList)
//                        .setOnClickSaveListener(object :
//                            TOListPopup.OnClickSaveListener {
//                            override fun clickSave(dataList: ArrayList<TOListBean>) {
//                                entity.data.turnOnsList = dataList
//                                setTurnOnsData(dataList)
//                            }
//
//                        })
//                }
            }

            override fun onFailure(code: Int, msg: String) {

            }
        })
        initListener()
    }

    fun setTurnOnsData(turnOnsList: ArrayList<TOListBean>?): ArrayList<TOListBean> {
        if (CollectionUtils.isNotEmpty(turnOnsList)) {
            viewBinding.viewTurnOnsUserInfo.isVisible = true
        } else {
            viewBinding.viewTurnOnsUserInfo.isVisible = false
            return ArrayList<TOListBean>()
        }
        val selecteTurnOnsData =
            turnOnsList?.filter { it.selected == 1 } as ArrayList<TOListBean>
        viewBinding.viewTurnOnsUserInfo.setData(selecteTurnOnsData)
        return turnOnsList
    }

    private fun initListener() {

        viewBinding.viewUserInfoUpImageView.addImageClickListener(object : ImageClickListener {
            override fun imageClick(isAdd: Boolean) {
                if (isAdd) {
                    SelectPhotoPopup(mActivity).showPopupWindow()
                } else {
                    mPresenter?.deleteImage()
                }

            }
        })
        viewBinding.conUserInfoNickNameContainer.setOnClickListener {
            NickNameModifyPopup(
                this,
                viewBinding.editUserInfoEditName.text.toString(),
                object : NickNameModifyPopup.onCallBack {
                    override fun onCallback(name: String) {
                        viewBinding.editUserInfoEditName.text = name
                        mPresenter?.saveProfileInfo(nickName = name)
                    }

                }).showPopupWindow()
        }
        viewBinding.imgViewUserInfoModifyAboutMe.setOnClickListener {
            UpdateAboutPopup(
                this,
                viewBinding.txtUserInfoAboutMeValue.text.toString(),
                object : UpdateAboutPopup.onCallBack {
                    override fun onCallback(value: String) {
                        viewBinding.txtUserInfoAboutMeValue.text = value
                        mPresenter?.saveProfileInfo(aboutMe = value)
                    }

                }).showPopupWindow()
        }
    }

    fun setGenderText(gender: Int) {
        viewBinding.txtUserInfoGender.text = TextUtil().addIconsSize(
            mActivity, mActivity.getString(
                when (gender) {
                    1 -> {
                        R.string.male
                    }

                    2 -> {
                        R.string.female
                    }

                    else -> {
                        R.string.gender_queer
                    }
                }
            ),
            when (gender) {
                1 -> R.mipmap.icon_male
                2 -> R.mipmap.icon_female
                else -> R.mipmap.icon_gay_sex
            }, 22f
        )
    }

    fun setLookingForDrawables(lookingFor: Int) {
        viewBinding.txtUserInfoLookingFor.text = TextUtil().addIconsSize(
            mActivity, mActivity.getString(
                when (lookingFor) {
                    1 -> {
                        R.string.male
                    }

                    2 -> {
                        R.string.female
                    }

                    else -> {
                        R.string.gender_queer
                    }
                }
            ),
            when (lookingFor) {
                1 -> R.mipmap.icon_male
                2 -> R.mipmap.icon_female
                else -> R.mipmap.icon_gay_sex
            }, 22f
        )
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        mPresenter?.onActivityResult(requestCode, resultCode, data)
    }

    override fun onResume() {
        super.onResume()
        ImmersionBar.with(mActivity).statusBarDarkFont(false).init()
    }

    override val viewUpImageViews: UpImageView
        get() = viewBinding.viewUserInfoUpImageView
    override val evEditName: TextView
        get() = viewBinding.editUserInfoEditName
    override val tvNowIwant: TextView
        get() = viewBinding.tvNowIwant
    override val viewYouAcceptList: LinearLayout
        get() = viewBinding.viewYouAcceptList
    override val tvEmptyTagYouAccept: TextView
        get() = viewBinding.tvEmptyTagYouAccept
    override val tvBirth: TextView
        get() = viewBinding.tvBirth
    override val imgInterestsAdd: ImageView
        get() = viewBinding.imgInterestsAdd
    override val viewTagCloud: TagCloudView
        get() = viewBinding.viewTagCloud
    override val interestsEmpty: TextView
        get() = viewBinding.interestsEmpty
    override val tvHeight: TextView
        get() = viewBinding.tvHeight
    override val txtAboutMeValue: TextView
        get() = viewBinding.txtUserInfoAboutMeValue
    override val date_of_birth_container: ConstraintLayout
        get() = viewBinding.dateOfBirthContainer
    override val looking_for_container: ConstraintLayout
        get() = viewBinding.lookingForContainer
    override val heightContainer: ConstraintLayout
        get() = viewBinding.heightContainer
    override val imgIWantAdd: ImageView
        get() = viewBinding.imgIWantAdd
    override val imgYourAcceptAdd: ImageView
        get() = viewBinding.imgYourAcceptAdd

    override val tvMiddleTitle: TextView?
        get() = viewBinding.viewTitle.tvMiddleTitle
    override val tvRightTitle: TextView?
        get() = viewBinding.viewTitle.tvRightTitle
    override val ivLeftImage: ImageView?
        get() = viewBinding.viewTitle.ivBackIcon
    override val viewStatusHeight: View?
        get() = viewBinding.viewTitle.viewStatusHeight

}